import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PrintNumbersAndLetters{

    public static void main(String[] args){
        Printer printer = new Printer();

        Thread t1 = new Thread(() -> {
            for(int i=1;i<=52;i+=2){
                printer.printNumbers(i,i+1);
            }
        });

        Thread t2 = new Thread(() -> {
            for(char c='A';c <='Z';c++){
                printer.printLetter(c);
            }
        });

        t1.start();
        t2.start();

        try{
            t1.join();
            t2.join();
        }catch(InterruptedException e){
            Thread.currentThread().interrupt();
        }

    }

}

class Printer{
    private final Lock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();
    private boolean printNumbers = true;

    public void printNumbers(int num1,int num2){
        lock.lock();
        try{
            while(!printNumbers){
                condition.await();
            }
            System.out.print(num1);
            System.out.print(num2);
            printNumbers = false;
            condition.signalAll();
        }catch(InterruptedException e){
            Thread.currentThread().interrupt();
        }finally{
            lock.unlock();
        }
    }

    public void printLetter(char letter){
        lock.lock();
        try{
            while(printNumbers){
                condition.await();
            }
            System.out.print(letter);
            printNumbers = true;
            condition.signalAll();
        }catch(InterruptedException e){
            Thread.currentThread().interrupt();
        }finally{
            lock.unlock();
        }
    }
}